$(function() {	
	
	subsystemID = 0;
	componentID = 0;
	subcomponentID = 0;
	deviceID = 0;
	
	menuSubsystem = $('#main_menu_div');
	var accordion_head = $('.maccordion > li > a'),
		accordion_body = $('.maccordion li > .msub-menu');

	menuSubsystem.find('#tools').addClass('active').next().slideDown("slow");

	accordion_head.on('click', function(event) {				
		// Show and hide the tabs on click	
		if ($(this).attr('class') != 'active'){
			accordion_body.slideUp("normal");
			$(this).next().stop(true,true).slideToggle("normal");
			accordion_head.removeClass('active');
			$(this).addClass('active');
		}
	}); 
	
	
	// Process Form Data	
	var fromTime = new Date();
	var toTime   = new Date();	
	var fromDate = new Date();
	var toDate   = new Date();
	fromDate.setDate(fromDate.getDate() -7);
	toDate.setDate(toDate.getDate() +1);
	fromDate = $.datepicker.formatDate('yy/mm/dd', fromDate);
	toDate = $.datepicker.formatDate('yy/mm/dd', toDate);
	fromTime = [fromTime.getHours(), fromTime.getMinutes(), fromTime.getSeconds()].join(':');
	toTime = [toTime.getHours(), toTime.getMinutes(), toTime.getSeconds()].join(':');		
	$('#cco_search #fromDate').attr('value', fromDate);
	$('#cco_search #toDate').attr('value', toDate);
	$('#cco_search #fromTime').attr('value', fromTime);
	$('#cco_search #toTime').attr('value', toTime);			    

	$('#cco_search_button').on( 'click',function() {
		$('#cco_search').find('.green').removeClass('green');	
		ccoTitle = 'CCO Details - General';   
		fetchCCOs();
	});
	$('#cco_search_clear' ).on( 'click',function() { 
		$(this).closest('form').find("input[type=text], textarea").val("");
 	});
 	
	// Statistical Data
	$('#btn_cco_my').on('click', function() {
		// $('#cco_search').find('.green').removeClass('green');	    
		// $('#cco_my a').toggleClass('green');
		$('#cco_search #status_id').attr('value', '');
		ccoTitle = 'CCO Details - My CCOs';  
		fetchMyCCOs();
	});
	
	$('#btn_cco_open').on('click', function() {		
		ccoTitle = 'CCO Details - Open CCOs';  
		var fromTime = new Date();
		var toTime   = new Date();	
		var fromDate = new Date();
		var toDate   = new Date();
		fromDate.setDate(fromDate.getDate() -365);
		toDate.setDate(toDate.getDate() +1);
		fromDate = $.datepicker.formatDate('yy/mm/dd', fromDate);
		toDate = $.datepicker.formatDate('yy/mm/dd', toDate);
		fromTime = [fromTime.getHours(), fromTime.getMinutes(), fromTime.getSeconds()].join(':');
		toTime = [toTime.getHours(), toTime.getMinutes(), toTime.getSeconds()].join(':');		
		$('#cco_search #fromDate').attr('value', fromDate);
		$('#cco_search #toDate').attr('value', toDate);
		$('#cco_search #fromTime').attr('value', fromTime);
		$('#cco_search #toTime').attr('value', toTime);			
		$('#cco_search #status_id').attr('value', 'Open');
		$('#cco_search').find('.green').removeClass('green');	    
		$('#cco_open a').toggleClass('green');
		fetchCCOs();
	});
	$('#btn_cco_closed').on('click', function() {	
		ccoTitle = 'CCO Details - Closed CCOs';  
		var fromTime = new Date();
		var toTime   = new Date();	
		var fromDate = new Date();
		var toDate   = new Date();
		fromDate.setDate(fromDate.getDate() -365);
		toDate.setDate(toDate.getDate() +1);
		var fromDate = $.datepicker.formatDate('yy/mm/dd', fromDate);
		var toDate = $.datepicker.formatDate('yy/mm/dd', toDate);
		fromTime = [fromTime.getHours(), fromTime.getMinutes(), fromTime.getSeconds()].join(':');
		toTime = [toTime.getHours(), toTime.getMinutes(), toTime.getSeconds()].join(':');		
		$('#cco_search #fromDate').attr('value', fromDate);
		$('#cco_search #toDate').attr('value', toDate);
		$('#cco_search #fromTime').attr('value', fromTime);
		$('#cco_search #toTime').attr('value', toTime);			
		$('#cco_search #status_id').attr('value', 'Closed');
		$('#cco_search').find('.green').removeClass('green');	    
		$('#cco_closed a').toggleClass('green');		
		fetchCCOs();
	});	
	$('#btn_cco_30d').on('click', function() {	
		ccoTitle = 'CCO Details - Last 30 Days';  	
		var fromTime = new Date();
		var toTime   = new Date();
		var fromDate = new Date();
		fromDate.setDate(fromDate.getDate() -30);
		var fromDate = $.datepicker.formatDate('yy/mm/dd', fromDate);
		var toDate = $.datepicker.formatDate('yy/mm/dd', new Date());		
		fromTime = [fromTime.getHours(), fromTime.getMinutes(), fromTime.getSeconds()].join(':');
		toTime = [toTime.getHours(), toTime.getMinutes(), toTime.getSeconds()].join(':');		
		$('#cco_search #fromDate').attr('value', fromDate);
		$('#cco_search #toDate').attr('value', toDate);
		$('#cco_search #fromTime').attr('value', fromTime);
		$('#cco_search #toTime').attr('value', toTime);	
		$('#cco_search #status_id').attr('value', '');
		$('#cco_search').find('.green').removeClass('green');	    
		$('#cco_30d a').toggleClass('green');			
	    fetchCCOs();
	});		
 
		
	function fetchCCOs(){
		var form = $('#cco_search');
		console.log (form.serialize());
		
		$.ajax({
			url: 		'cco/get_ccos',
			type: 		'POST', 
			data:		form.serialize(),
			dataType: 	'json',
			success: 	processCCOs
		})	
	}
	
	function fetchMyCCOs(){
		var form = $('#cco_search');
		console.log (form.serialize());
		
		$.ajax({
			url: 		'cco/get_my_ccos',
			type: 		'POST', 
			data:		form.serialize(),
			dataType: 	'json',
			success: 	processCCOs
		})	
	}

	function processCCOs(results){
        if (results.message == 'ok'){
        	$('#cco_main').html(results.html); 
		 	$("#cco_main select").uniform();  
		 	$('#cco_details_title').text(ccoTitle);	
			
			$.fn.contentTabs = function(){ 
		
			$(this).find(".tab_content").hide(); //Hide all content
			$(this).find("ul.tabs li:first").addClass("activeTab").show(); //Activate first tab
			$(this).find(".tab_content:first").show(); //Show first tab content
		
			$("ul.tabs li").click(function() {
				$(this).parent().parent().find("ul.tabs li").removeClass("activeTab"); //Remove any "active" class
				$(this).addClass("activeTab"); //Add "active" class to selected tab
				$(this).parent().parent().find(".tab_content").hide(); //Hide all tab content
				var activeTab = $(this).find("a").attr("href"); //Find the rel attribute value to identify the active tab + content
				$(activeTab).show(); //Fade in the active content
				return false;
			});
		
			};
			$("div[class^='widget']").contentTabs(); 	
			
			$("#tab_cco_tbl tbody").on('click', 'tr', (function(event) {  	        	
	        	$('#tab_cco_tbl').find('.thisRow').removeClass('thisRow');	        	
				$(this).addClass('thisRow');
				
				ccoID 				  = $(this).find('#cco_id_td').text(); 
				subsystemName  	  	  = $(this).find('#subsystem_nm_td').text();
				componentName  	  	  = $(this).find('#component_nm_td').text();
				subcomponentName   	  = $(this).find('#subcomponent_nm_td').text();
				hostName  		  	  = $(this).find('#host_nm_td').text();
				ccoMttsTicketID 	  = $(this).find('#mtts_id_td').text(); 
				ccoSeverityID 		  = $(this).find('#severity_id_nm').text();
				ccoDescription 		  = $(this).find('#description_td').text();	
				ccoReceivedDt 		  = $(this).find('#received_dt_td').text();
				ccoDueDt 			  = $(this).find('#due_dt_td').text();
				ccoScheduledInstallDt = $(this).find('#scheduled_install_dt_td').text();
				ccoScheduledShiftID	  = $(this).find('#scheduled_install_shift_id').text();
				ccoInstallDt 		  = $(this).find('#install_dt_td').text();
				ccoInstalledBy 		  = $(this).find('#installed_by_td').text();
				ccoStatus 			  = $(this).find('#status_nm_td').text();
				ccoStatusNotes 		  = $(this).find('#notes_td').text();
														
			})); 	
			
			//===== Dialog Link based on Selection ===== //
		    $('#cco_dropdown li #cco_add').click(function () {
		        $('#cco_dialog').dialog('open');
		        $('#cco_success').hide();
		        $('#cco_failure').hide();
				startDateTimeFields();	               
		    });
	
		    $('#cco_widget #cco_widget_add').click(function () {
		        $('#cco_dialog').dialog('open');
		        $('#cco_success').hide();
		        $('#cco_failure').hide(); 
		        startDateTimeFields();						        	                 
		    });
	
		    $('#cco_dropdown li #cco_edit').click(function () {
		        $('#cco_dialog_edit').dialog('open');
		        $('#cco_edit_success').hide();
		        $('#cco_edit_failure').hide();
		        setDateTimeFields();
		        $('#cco_dialog_edit #cco_details_id').attr('value', ccoID);
		        $('#cco_dialog_edit #cco_id').attr('value', ccoID); 
		        $('#cco_dialog_edit #mtts_ticket_id').attr('value', ccoMttsTicketID); 		        
		        $("#cco_dialog_edit #severity_id option:contains(" + ccoSeverityID + ")").attr('selected', 'selected');	       		        
		        $('#cco_dialog_edit #cco_description').attr('value', ccoDescription); 		        
		        $("#cco_dialog_edit #scheduled_shift_id option:contains(" + ccoScheduledShiftID + ")").attr('selected', 'selected');		        		        
		        $("#cco_dialog_edit #status_id option:contains(" + ccoStatus + ")").attr('selected', 'selected');		        
		        $('#cco_dialog_edit #status_notes').attr('value', ccoStatusNotes); 	 	             
		    });
	
		    $('#cco_dropdown li #cco_delete').click(function () {
		        $('#cco_dialog_delete').dialog('open');
		        $('#cco_delete_success').hide();
		        $('#cco_delete_failure').hide();
		        setDateTimeFieldsDelete();
		       	$('#cco_dialog_delete #cco_details_id').attr('value', ccoID);
		        $('#cco_dialog_delete #mtts_ticket_id').attr('value', ccoMttsTicketID); 		        
		        $("#cco_dialog_delete #severity_id option:contains(" + ccoSeverityID + ")").attr('selected', 'selected');	       		        
		        $('#cco_dialog_delete #cco_description').attr('value', ccoDescription); 		        
		        $("#cco_dialog_delete #scheduled_shift_id option:contains(" + ccoScheduledShiftID + ")").attr('selected', 'selected');		        		        
		        $("#cco_dialog_delete #status_id option:contains(" + ccoStatus + ")").attr('selected', 'selected');		        
		        $('#cco_dialog_delete #status_notes').attr('value', ccoStatusNotes); 	  
		    });	
		    
		    function startDateTimeFields() {
		    	$('#cco_dialog #received_dt').datepicker({dateFormat:"yy-mm-dd"}).datepicker("setDate",new Date()); 
		        $('#cco_dialog #received_ts').timeEntry({
					show24Hours: true, // 24 hours format
					showSeconds: true, // Show seconds?
					defaultTime: new Date(),
					spinnerImage: '../../assets/images/elements/ui/spinner.png', // Arrows image
					spinnerSize: [19, 26, 0], // Image size
					spinnerIncDecOnly: true // Only up and down arrows
				});	
		        $('#cco_dialog #due_dt').datepicker({dateFormat:"yy-mm-dd"}).datepicker("setDate",new Date()); 	
	        	$('#cco_dialog #due_ts').timeEntry({
					show24Hours: true, // 24 hours format
					showSeconds: true, // Show seconds?
					defaultTime: new Date(),
					spinnerImage: '../../assets/images/elements/ui/spinner.png', // Arrows image
					spinnerSize: [19, 26, 0], // Image size
					spinnerIncDecOnly: true // Only up and down arrows
				});	
		        $('#cco_dialog #install_dt').datepicker({dateFormat:"yy-mm-dd"}); 	
	        	$('#cco_dialog #install_ts').timeEntry({
					show24Hours: true, // 24 hours format
					showSeconds: true, // Show seconds?
					defaultTime: new Date(),
					spinnerImage: '../../assets/images/elements/ui/spinner.png', // Arrows image
					spinnerSize: [19, 26, 0], // Image size
					spinnerIncDecOnly: true // Only up and down arrows
				});									
		        $('#cco_dialog #scheduled_install_dt').datepicker({dateFormat:"yy-mm-dd"}); 
	        	$('#cco_dialog #scheduled_install_ts').timeEntry({
					show24Hours: true, // 24 hours format
					showSeconds: true, // Show seconds?
					defaultTime: new Date(),
					spinnerImage: '../../assets/images/elements/ui/spinner.png', // Arrows image
					spinnerSize: [19, 26, 0], // Image size
					spinnerIncDecOnly: true // Only up and down arrows
				});	
				
				$('.ui-timepicker').css('float','left');			
				var createTime = new Date();
				createTime = [createTime.getHours(), createTime.getMinutes(), createTime.getSeconds()].join(':');
				$('#cco_dialog #received_ts').attr('value', createTime);	
				$('#cco_dialog #due_ts').attr('value', createTime);	
				//$('#cco_dialog #install_ts').attr('value', createTime);	
				//$('#cco_dialog #scheduled_install_ts').attr('value', createTime);		
		    }		

		    function setDateTimeFields() {
		    	$('#cco_dialog_edit #received_dt').datepicker({dateFormat:"yy-mm-dd"}).datepicker("setDate",new Date(ccoReceivedDt)); 
		        $('#cco_dialog_edit #received_ts').timeEntry({
					show24Hours: true, // 24 hours format
					showSeconds: true, // Show seconds?
					defaultTime: new Date(),
					spinnerImage: '../../assets/images/elements/ui/spinner.png', // Arrows image
					spinnerSize: [19, 26, 0], // Image size
					spinnerIncDecOnly: true // Only up and down arrows
				});	
		        $('#cco_dialog_edit #due_dt').datepicker({dateFormat:"yy-mm-dd"}).datepicker("setDate",new Date(ccoDueDt)); 	
	        	$('#cco_dialog_edit #due_ts').timeEntry({
					show24Hours: true, // 24 hours format
					showSeconds: true, // Show seconds?
					defaultTime: new Date(),
					spinnerImage: '../../assets/images/elements/ui/spinner.png', // Arrows image
					spinnerSize: [19, 26, 0], // Image size
					spinnerIncDecOnly: true // Only up and down arrows
				});	
		        $('#cco_dialog_edit #install_dt').datepicker({dateFormat:"yy-mm-dd"}).datepicker("setDate",new Date(ccoInstallDt));	
	        	$('#cco_dialog_edit #install_ts').timeEntry({
					show24Hours: true, // 24 hours format
					showSeconds: true, // Show seconds?
					defaultTime: new Date(),
					spinnerImage: '../../assets/images/elements/ui/spinner.png', // Arrows image
					spinnerSize: [19, 26, 0], // Image size
					spinnerIncDecOnly: true // Only up and down arrows
				});									
		        $('#cco_dialog_edit #scheduled_install_dt').datepicker({dateFormat:"yy-mm-dd"}).datepicker("setDate",new Date(ccoScheduledInstallDt)); 
	        	$('#cco_dialog_edit #scheduled_install_ts').timeEntry({
					show24Hours: true, // 24 hours format
					showSeconds: true, // Show seconds?
					defaultTime: new Date(),
					spinnerImage: '../../assets/images/elements/ui/spinner.png', // Arrows image
					spinnerSize: [19, 26, 0], // Image size
					spinnerIncDecOnly: true // Only up and down arrows
				});	
				
				$('.ui-timepicker').css('float','left');			
				var createTime = new Date();
				createTime = [createTime.getHours(), createTime.getMinutes(), createTime.getSeconds()].join(':');
				$('#cco_dialog_edit #received_ts').attr('value', createTime);	
				$('#cco_dialog_edit #due_ts').attr('value', createTime);	
				$('#cco_dialog_edit #install_ts').attr('value', createTime);	
				$('#cco_dialog_edit #scheduled_install_ts').attr('value', createTime);		
		    }		

		    function setDateTimeFieldsDelete() {
		    	$('#cco_dialog_delete #received_dt').datepicker({dateFormat:"yy-mm-dd"}).datepicker("setDate",new Date(ccoReceivedDt)); 
		        $('#cco_dialog_delete #received_ts').timeEntry({
					show24Hours: true, // 24 hours format
					showSeconds: true, // Show seconds?
					defaultTime: new Date(),
					spinnerImage: '../../assets/images/elements/ui/spinner.png', // Arrows image
					spinnerSize: [19, 26, 0], // Image size
					spinnerIncDecOnly: true // Only up and down arrows
				});	
		        $('#cco_dialog_delete #due_dt').datepicker({dateFormat:"yy-mm-dd"}).datepicker("setDate",new Date(ccoDueDt)); 	
	        	$('#cco_dialog_delete #due_ts').timeEntry({
					show24Hours: true, // 24 hours format
					showSeconds: true, // Show seconds?
					defaultTime: new Date(),
					spinnerImage: '../../assets/images/elements/ui/spinner.png', // Arrows image
					spinnerSize: [19, 26, 0], // Image size
					spinnerIncDecOnly: true // Only up and down arrows
				});	
		        $('#cco_dialog_delete #install_dt').datepicker({dateFormat:"yy-mm-dd"}).datepicker("setDate",new Date(ccoInstallDt));	
	        	$('#cco_dialog_delete #install_ts').timeEntry({
					show24Hours: true, // 24 hours format
					showSeconds: true, // Show seconds?
					defaultTime: new Date(),
					spinnerImage: '../../assets/images/elements/ui/spinner.png', // Arrows image
					spinnerSize: [19, 26, 0], // Image size
					spinnerIncDecOnly: true // Only up and down arrows
				});									
		        $('#cco_dialog_delete #scheduled_install_dt').datepicker({dateFormat:"yy-mm-dd"}).datepicker("setDate",new Date(ccoScheduledInstallDt)); 
	        	$('#cco_dialog_delete #scheduled_install_ts').timeEntry({
					show24Hours: true, // 24 hours format
					showSeconds: true, // Show seconds?
					defaultTime: new Date(),
					spinnerImage: '../../assets/images/elements/ui/spinner.png', // Arrows image
					spinnerSize: [19, 26, 0], // Image size
					spinnerIncDecOnly: true // Only up and down arrows
				});	
				
				$('.ui-timepicker').css('float','left');			
				var createTime = new Date();
				createTime = [createTime.getHours(), createTime.getMinutes(), createTime.getSeconds()].join(':');
				$('#cco_dialog_delete #received_ts').attr('value', createTime);	
				$('#cco_dialog_delete #due_ts').attr('value', createTime);	
				$('#cco_dialog_delete #install_ts').attr('value', createTime);	
				$('#cco_dialog_delete #scheduled_install_ts').attr('value', createTime);		
		    }		

			
			// Dialog - Add
		    $('#cco_dialog').dialog({
		        autoOpen: false,
		        width: 600,
		        modal: true,
		       	open: function(event, ui) {
		        	s1 = $('#subsystem_dropdown_div').empty();
		        	s2 = $('#component_dropdown_div');
		        	s3 = $('#subcomponent_dropdown_div');
		        	s4 = $('#device_dropdown_div');
		        	s1[0].enabled = false;
		        	s2[0].enabled = false;
		        	s3[0].enabled = false;
		        	s4[0].enabled = false;
		        	
					$.ajax({
						url: 		'../subsystem/subsystem/get_subsystem_dropdown',
						type: 		'POST', 
						dataType: 	'json',
						success: 	function (results) {
										s1.html(results.html);
										$("#subsystem_dropdown_div select").uniform(); 
				                        s1[0].enabled = true;
				                    }
					});	
					
					subsystemID = $(this).find(':selected').attr('id');
					
					$.ajax({
					url: 		'../subsystem/subsystem/get_component_dropdown',
					type: 		'POST', 
					dataType: 	'json',
					data:		{'subsystem_id': subsystemID},
					success: 	function (results) {
			                        s2.empty().html(results.html);
			                        $("#component_dropdown_div select").uniform(); 
			                        s2[0].enabled = true;
			                    }
					});	
					
					s1.on('change', function(){
						subsystemID = $(this).find(':selected').attr('id');
						fetchComponents( subsystemID );
						$("#subcomponent_dropdown_div #subcomponent_nm_i option:contains(" + 'NA' + ")").attr('selected', 'selected'); 	
						$('#subcomponent_dropdown_div #uniform-subcomponent_nm_i span').text('NA');
						$("#device_dropdown_div #device_nm_i option:contains(" + 'NA' + ")").attr('selected', 'selected'); 	
						$('#device_dropdown_div #uniform-device_nm_i span').text('NA');
					})
					
	
					componentID = $(this).find(':selected').attr('id');
					
					$.ajax({
					url: 		'../subsystem/subsystem/get_subcomponent_dropdown',
					type: 		'POST', 
					dataType: 	'json',
					data:		{'component_id': componentID},
					success: 	function (results) {
			                        s3.empty().html(results.html);
			                        $("#subcomponent_dropdown_div select").uniform(); 
			                        s3[0].enabled = true;
			                    }
					});		
	
					s2.on('change', function(){
						componentID = $(this).find(':selected').attr('id');
						fetchSubcomponents( componentID );
						s4.val( s4.prop('defaultSelected') );
					})
	
					
					subcomponentID = $(this).find(':selected').attr('id');
					
					$.ajax({
					url: 		'../subsystem/subsystem/get_device_dropdown',
					type: 		'POST', 
					dataType: 	'json',
					data:		{'subcomponent_id': subcomponentID},
					success: 	function (results) {
			                        s4.empty().html(results.html);
			                        $("#device_dropdown_div select").uniform(); 
			                        s4[0].enabled = true;
			                    }
					});	
					
					s3.on('change', function(){
						subcomponentID = $(this).find(':selected').attr('id');
						fetchDevices( subcomponentID );
					})							
					
					
		        },				
		        
		        buttons: {
		            "Save": function () {
		            			var form = $('#cco_add_form');
		            			// console.log ('form ' + form.serialize());												
								$.ajax({
									url: 		'cco/add_cco',
									type: 		'POST', 
									data:		form.serialize(),
									dataType: 	'json',
									success: 	addCcoResults
								})	
		            },
		            "Cancel": function () {
		                $(this).dialog("close");
		            }
		        }
		    });		

			function fetchComponents(subsystemID) {
				
				//subsystemID = $(this).find(':selected').attr('id');
		       	s2[0].enabled = false;			
				
				$.ajax({
				url: 		'../subsystem/subsystem/get_component_dropdown',
				type: 		'POST', 
				dataType: 	'json',
				data:		{'subsystem_id': subsystemID},
				success: 	function (results) {
		                        s2.empty().html(results.html);
		                        $("#component_dropdown_div select").uniform(); 
		                        $("#component_dropdown_edit_div select").uniform(); 
		                        s2[0].enabled = true;
		                    }
				});			       	
									
			}	    		
	
			function fetchSubcomponents( componentID ) {
				
				//componentID = $(this).find(':selected').attr('id');
				
		       	s3[0].enabled = false;			
				
					$.ajax({
					url: 		'../subsystem/subsystem/get_subcomponent_dropdown',
					type: 		'POST', 
					dataType: 	'json',
					data:		{'component_id': componentID},
					success: 	function (results) {
			                        s3.empty().html(results.html);
			                        $("#subcomponent_dropdown_div select").uniform(); 
			                        $("#subcomponent_dropdown_edit_div select").uniform();
			                        s3[0].enabled = true;
			                    }
					});					
			}
	
			function fetchDevices( subcomponentID ) {
				
				//subcomponentID = $(this).find(':selected').attr('id');
				
		       	s4[0].enabled = false;			
				
				$.ajax({
				url: 		'../subsystem/subsystem/get_device_dropdown',
				type: 		'POST', 
				dataType: 	'json',
				data:		{'subcomponent_id': subcomponentID},
				success: 	function (results) {
		                        s4.empty().html(results.html);
		                        $("#device_dropdown_div select").uniform(); 
		                        $("#device_dropdown_edit_div select").uniform(); 
		                        s4[0].enabled = true;
		                    }
				});					
			}
	
			function addCcoResults(results){
				$('#cco_success').hide();
				$('#cco_failure').hide();
		        if (results.ret == 'ok' ){	
		        	$('#cco_add_form').find("input[type=text], textarea").val("");
		        	$('#cco_success').show();	
		        }
		        else{
		        	$('#cco_failure').show();
		        }	
		
		    	$(".nNote").click(function() {
					$(this).fadeTo(200, 0.00, function(){ //fade
						$(this).slideUp(200, function() { //slide up
							$(this).remove(); //then remove from the DOM
						});
					});
				});	
				
			}		
	
			// Dialog - Edit
		    $('#cco_dialog_edit').dialog({
		        autoOpen: false,
		        width: 600,
		        modal: true,
		       	open: function(event, ui) {
		        	
		        	s1 = $('#subsystem_dropdown_edit_div').empty();
		        	s2 = $('#component_dropdown_edit_div');
		        	s3 = $('#subcomponent_dropdown_edit_div');
		        	s4 = $('#device_dropdown_edit_div');
		        	s1[0].enabled = false;
		        	s2[0].enabled = false;
		        	s3[0].enabled = false;
		        	s4[0].enabled = false;
		        	
					$.ajax({
						url: 		'../subsystem/subsystem/get_subsystem_dropdown',
						type: 		'POST', 
						dataType: 	'json',
						success: 	function (results) {
										s1.html(results.html);
										$("#subsystem_dropdown_edit_div select").uniform(); 
										$('#subsystem_dropdown_edit_div #uniform-subsystem_nm_i span').text(subsystemName); 
										$("#subsystem_dropdown_edit_div #subsystem_nm_i option:contains(" + subsystemName + ")").attr('selected', 'selected');
										
				                        s1[0].enabled = true;
	
				                        subsystemID = s1.find(':selected').attr('id');	 								
										
										$.ajax({
										url: 		'../subsystem/subsystem/get_component_dropdown',
										type: 		'POST', 
										dataType: 	'json',
										data:		{'subsystem_id': subsystemID},
										success: 	function (results) {
								                        s2.empty().html(results.html);
														$("#component_dropdown_edit_div select").uniform(); 
														$('#component_dropdown_edit_div #uniform-component_nm_i span').text(componentName); 
														$("#component_dropdown_edit_div #component_nm_i option:contains(" + componentName + ")").attr('selected', 'selected'); 	
																									
								                        s2[0].enabled = true;
								                        
														componentID = s2.find(':selected').attr('id');
														
														$.ajax({
														url: 		'../subsystem/subsystem/get_subcomponent_dropdown',
														type: 		'POST', 
														dataType: 	'json',
														data:		{'component_id': componentID},
														success: 	function (results) {
												                        s3.empty().html(results.html);
												                        $("#subcomponent_dropdown_edit_div select").uniform(); 
																		$("#subcomponent_dropdown_edit_div #subcomponent_nm_i option:contains(" + subcomponentName + ")").attr('selected', 'selected'); 	
																		$('#subcomponent_dropdown_edit_div #uniform-subcomponent_nm_i span').text(subcomponentName);							                        
												                        s3[0].enabled = true;
												                        
												                        subcomponentID = s3.find(':selected').attr('id');
					
																		$.ajax({
																		url: 		'../subsystem/subsystem/get_device_dropdown',
																		type: 		'POST', 
																		dataType: 	'json',
																		data:		{'subcomponent_id': subcomponentID},
																		success: 	function (results) {
																                        s4.empty().html(results.html);
																                        $("#device_dropdown_edit_div select").uniform(); 
																                        $("#device_dropdown_edit_div #device_nm_i option:contains(" + hostName + ")").attr('selected', 'selected'); 	
																						$('#device_dropdown_edit_div #uniform-device_nm_i span').text(hostName);
																                        s4[0].enabled = true;
																                    }
																		});																									                        											                        
												                    }
														});								                        							                        
								                    }
										});								
				                    }
					});	
		
					
					s1.on('change', function(){
						subsystemID = $(this).find(':selected').attr('id');
						fetchComponents( subsystemID );
						$("#subcomponent_dropdown_edit_div #subcomponent_nm_i option:contains(" + 'NA' + ")").attr('selected', 'selected'); 	
						$('#subcomponent_dropdown_edit_div #uniform-subcomponent_nm_i span').text('NA');
						$("#device_dropdown_div #device_nm_i option:contains(" + 'NA' + ")").attr('selected', 'selected'); 	
						$('#device_dropdown_div #uniform-device_nm_i span').text('NA');
					});
					
					s2.on('change', function(){
						componentID = $(this).find(':selected').attr('id');
						fetchSubcomponents( componentID );
						s4.val( s4.prop('defaultSelected') );
						$("#device_dropdown_div #device_nm_i option:contains(" + 'NA' + ")").attr('selected', 'selected'); 	
						$('#device_dropdown_div #uniform-device_nm_i span').text('NA');
					});
					
					s3.on('change', function(){
						subcomponentID = $(this).find(':selected').attr('id');
						fetchDevices( subcomponentID );
					});	
				
				},       
		        
		        buttons: {
		            "Save": function () {
		            			var form = $('#cco_edit_form');
		            			console.log ('form ' + form.serialize());	
								
								$.ajax({
									url: 		'cco/edit_cco',
									type: 		'POST', 
									data:		form.serialize(),
									dataType: 	'json',
									success: 	editCcoResults
								})	
		            },
		            "Cancel": function () {
		                $(this).dialog("close");
		            }
		        }
		    });		
	
			function editCcoResults(results){
				$('#cco_edit_success').hide();
				$('#cco_edit_failure').hide();
		        if (results.ret == 'ok' ){	
		        	$('#cco_edit_form').find("input[type=text], textarea").val("");
		        	$('#cco_edit_success').show();	
		        }
		        else{
		        	$('#cco_edit_failure').show();
		        }	
		
		    	$(".nNote").click(function() {
					$(this).fadeTo(200, 0.00, function(){ //fade
						$(this).slideUp(200, function() { //slide up
							$(this).remove(); //then remove from the DOM
						});
					});
				});	
				
			}


			// Dialog - Delete
		    $('#cco_dialog_delete').dialog({
		        autoOpen: false,
		        width: 600,
		        modal: true,
		       	open: function(event, ui) {
		        	
		        	s1 = $('#subsystem_dropdown_delete_div').empty();
		        	s2 = $('#component_dropdown_delete_div');
		        	s3 = $('#subcomponent_dropdown_delete_div');
		        	s4 = $('#device_dropdown_delete_div');
		        	s1[0].enabled = false;
		        	s2[0].enabled = false;
		        	s3[0].enabled = false;
		        	s4[0].enabled = false;
		        	
					$.ajax({
						url: 		'../subsystem/subsystem/get_subsystem_dropdown',
						type: 		'POST', 
						dataType: 	'json',
						success: 	function (results) {
										s1.html(results.html);
										$("#subsystem_dropdown_delete_div select").uniform(); 
										$('#subsystem_dropdown_delete_div #uniform-subsystem_nm_i span').text(subsystemName); 
										$("#subsystem_dropdown_delete_div #subsystem_nm_i option:contains(" + subsystemName + ")").attr('selected', 'selected');
										
				                        s1[0].enabled = false;
	
				                        subsystemID = s1.find(':selected').attr('id');	 								
										
										$.ajax({
										url: 		'../subsystem/subsystem/get_component_dropdown',
										type: 		'POST', 
										dataType: 	'json',
										data:		{'subsystem_id': subsystemID},
										success: 	function (results) {
								                        s2.empty().html(results.html);
														$("#component_dropdown_delete_div select").uniform(); 
														$('#component_dropdown_delete_div #uniform-component_nm_i span').text(componentName); 
														$("#component_dropdown_delete_div #component_nm_i option:contains(" + componentName + ")").attr('selected', 'selected'); 	
																									
								                        s2[0].enabled = false;
								                        
														componentID = s2.find(':selected').attr('id');
														
														$.ajax({
														url: 		'../subsystem/subsystem/get_subcomponent_dropdown',
														type: 		'POST', 
														dataType: 	'json',
														data:		{'component_id': componentID},
														success: 	function (results) {
												                        s3.empty().html(results.html);
												                        $("#subcomponent_dropdown_delete_div select").uniform(); 
																		$("#subcomponent_dropdown_delete_div #subcomponent_nm_i option:contains(" + subcomponentName + ")").attr('selected', 'selected'); 	
																		$('#subcomponent_dropdown_delete_div #uniform-subcomponent_nm_i span').text(subcomponentName);							                        
												                        s3[0].enabled = false;
												                        
												                        subcomponentID = s3.find(':selected').attr('id');
					
																		$.ajax({
																		url: 		'../subsystem/subsystem/get_device_dropdown',
																		type: 		'POST', 
																		dataType: 	'json',
																		data:		{'subcomponent_id': subcomponentID},
																		success: 	function (results) {
																                        s4.empty().html(results.html);
																                        $("#device_dropdown_delete_div select").uniform(); 
																                        $("#device_dropdown_delete_div #device_nm_i option:contains(" + hostName + ")").attr('selected', 'selected'); 	
																						$('#device_dropdown_delete_div #uniform-device_nm_i span').text(hostName);
																                        s4[0].enabled = false;
																                    }
																		});																									                        											                        
												                    }
														});								                        							                        
								                    }
										});								
				                    }
					});	
				
				},       
		        
		        buttons: {
		            "Are You Sure?? Then Click Here": function () {
		            			var form = $('#cco_delete_form');
		            			console.log ('form ' + form.serialize());	
								
								$.ajax({
									url: 		'cco/delete_cco',
									type: 		'POST', 
									data:		form.serialize(),
									dataType: 	'json',
									success: 	deleteCcoResults
								})	
		            },
		            "Cancel": function () {
		                $(this).dialog("close");
		            }
		        }
		    });		
	
	
			function deleteCcoResults(results){
				$('#cco_delete_success').hide();
				$('#cco_delete_failure').hide();
		        if (results.ret == 'ok' ){	
		        	$('#cco_delete_form').find("input[type=text], textarea").val("");
		        	$('#cco_delete_success').show();	
		        }
		        else{
		        	$('#cco_delete_failure').show();
		        }	
		
		    	$(".nNote").click(function() {
					$(this).fadeTo(200, 0.00, function(){ //fade
						$(this).slideUp(200, function() { //slide up
							$(this).remove(); //then remove from the DOM
						});
					});
				});	
				
			}			
								
        }
        else{
            this.buildMsg('nNote nWarning','There was an error processing your request. Please try again.');
        }       
        
	};			

	$( ".datepicker" ).datepicker({ 
		defaultDate: +1,
		showOtherMonths:true,
		autoSize: true,
		appendText: '(dd-mm-yyyy)',
		dateFormat: 'dd-mm-yy'
	});	
	
	$(function() {
			var dates = $( "#fromDate, #toDate" ).datepicker({
				defaultDate: "+1d",
				changeMonth: false,
				showOtherMonths:true,
				numberOfMonths: 3,
				onSelect: function( selectedDate ) {
					var option = this.id == "fromDate" ? "minDate" : "maxDate",
						instance = $( this ).data( "datepicker" ),
						date = $.datepicker.parseDate(
							instance.settings.dateFormat ||
							$.datepicker._defaults.dateFormat,
							selectedDate, instance.settings );
					dates.not( this ).datepicker( "option", option, date );
				}
			});
		});
		
	//===== Time picker =====//
	
	$('.timepicker').timeEntry({
		show24Hours: true, // 24 hours format
		showSeconds: true, // Show seconds?
		spinnerImage: '../../assets/images/elements/ui/spinner.png', // Arrows image
		spinnerSize: [19, 26, 0], // Image size
		spinnerIncDecOnly: true // Only up and down arrows
	});	

	
});	
